package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * @author huangliusong
 * @since 2018/11/4
 * {@link }
 */
public class BatchTest {
    public static void main(String[] args) throws Exception{
        long sTime=System.currentTimeMillis();
        Connection connection=null;
        PreparedStatement preparedStatement= null;


        connection=JdbcUtil.getConnection();

        JdbcUtil.begin(connection);
        String sql="insert into t_user(username,address) values(?,?)";
        preparedStatement=connection.prepareStatement(sql);

        for (int i=0;i<10000;i++){
            preparedStatement.setString(1,"hls"+(i+1));
            preparedStatement.setString(2,"地址"+(i+1));
            if ((i+1)%1000==0){
                preparedStatement.addBatch();
                preparedStatement.executeBatch();
                preparedStatement.clearBatch();
            }
        }

        JdbcUtil.commit(connection);

        long eTime=System.currentTimeMillis();

        System.out.println("总耗时："+(eTime-sTime)+"ms");

    }
}
